<?php
App::uses('AppController', 'Controller');
/**
 * Index Controller
 *
 */
class IndexController extends AppController {
	
	public $uses = array();
/**
 * Components
 *
 * @var array
 */
	public $components = array('Auth','Paginator');

	public $helpers = array('Cache');
	
	public $cacheAction = array(
			//'index'  => 3600
	);
	
	public function beforeFilter(){
		$this->Auth->allow('index','eticket','eticketpub','autocomplete');
		parent::beforeFilter();
	}
	
	function eticket($hash = false){
		$this->layout = '';
		if($this->request->is('post')){
			$this->loadModel('User');
			$auth = $this->request->data['eticket']['auth'];
			$this->request->data['eticket']['auth'] = '';
			$checker = $this->User->findById(1);
			if(AuthComponent::password($auth) == $checker['User']['password']){
				$this->autoRender = false;
				$decode = (decodeET($hash));
				if(is_array($decode) == 'array'){
					$this->loadModel('UserOrderList');
					$this->UserOrderList->recursive = 3;
					$ticket = $this->UserOrderList->findById($decode['uoli']);
					if($ticket){
						$this->loadModel('UserOrder');
						$this->UserOrder->recursive = -1;
						$order = $this->UserOrder->findById($ticket['UserOrderList']['user_order_id']);
						$this->set('ticket',$ticket);
						$this->set('order',$order);
						$this->set('hash',$hash);
						$this->render('eticket');
					}else{
						$this->Session->setFlash(__('无效链接'),'tip/tip_eticket',array('t'=>'error'));
						$this->render('eticket_login');
					}
				}else{
					$this->Session->setFlash(__('无效链接'),'tip/tip_eticket',array('t'=>'error'));
					$this->render('eticket_login');
				}
			}else{
				$this->Session->setFlash(__('认证失败'),'tip/tip_eticket',array('t'=>'error'));
				$this->render('eticket_login');
			}
		}else{
			$this->render('eticket_login');
		}
		
	}
	
	function eticketpub($hash = false){
		$this->layout = '';
		if($this->request->is('post')){
			$this->loadModel('User');
			$auth = $this->request->data['eticket']['auth'];
			$checker = $this->User->findById(1);
			if(AuthComponent::password($auth) == $checker['User']['password']){
				$this->autoRender = false;
				$decode = (decodeET($hash));
				if(is_array($decode) == 'array'){
					$this->loadModel('UserOrderList');
					$this->UserOrderList->recursive = 3;
					//$this->UserOrderList->Ticket->recursive = 2;
					$ticket = $this->UserOrderList->findById($decode['uoli']);
					if($ticket){
						$this->UserOrderList->id = $ticket['UserOrderList']['user_order_id'];
						$this->UserOrderList->set('state',3);
						if($this->UserOrderList->save()){
							$this->Session->setFlash(__('兑换成功'),'tip/tip_eticket',array('t'=>'success'));
							$this->redirect(array('action'=>'eticket',$hash));
						}else{
							$this->Session->setFlash(__('兑换失败'),'tip/tip_eticket',array('t'=>'error'));
							$this->redirect(array('action'=>'eticket',$hash));
						}
					}else{
						$this->Session->setFlash(__('认证失败'),'tip/tip_eticket',array('t'=>'error'));
						$this->redirect(array('action'=>'eticket',$hash));
					}
				}else{
					$this->Session->setFlash(__('认证失败'),'tip/tip_eticket',array('t'=>'error'));
					$this->redirect(array('action'=>'eticket',$hash));
				}
			}else{
				$this->Session->setFlash(__('认证失败'),'tip/tip_eticket',array('t'=>'error'));
						$this->redirect(array('action'=>'eticket',$hash));
			}
		}else{
			$this->redirect(array('action'=>'eticket',$hash));
		}
	
	}
	
	/**
	 * website index
	 * @author alexanderzhao
	 * @date 2013-12-21 上午11:16:50
	 * @param variable
	 * @return 
	 */
	public function index() {
		
		$SITEID = 1;//Configure::read('SITEID');
		// 票品-------------------------------------------------------------
		$this->loadModel('Ticket');
		$this->Ticket->recursive = 2;
		$this->Ticket->unbindModel(array(
			'belongsTo'=>array('ViewPhoto','SysSeo','SysWebsite'),
			'hasMany'=>array('TicketPrice')
		),false);
		
		$this->loadModel('SysWebsite');
		$site_info = $this->SysWebsite->findById($SITEID);
		$cate = split(',',$site_info['SysWebsite']['cate']);
		$data = array();
		foreach ($cate as $cid){
			$data[$cid] = $this->Ticket->getCateTicket($cid);
		};
		$this->set('TicketsList',$data);
		
		// 最新看点推荐-------------------------------------------------------------
		$this->loadModel('Article');
		$this->Article->recursive = -1;
		$hotArticles = $this->Article->find('all',array(
			'conditions'=>array(
				'Article.recmd_index'=>1,
				'Article.article_cate_id <>'=>2,
				'Article.sys_website_id'=>$SITEID,
			    'Article.enable'=>1
			),
			'limit'=>12,
			'order'=>array('Article.publish DESC')
		));
		$this->set('hotArticles',$hotArticles);
		
		// 热门推荐-------------------------------------------------------------
		$hotTicket = $this->Ticket->find('all',array(
			'conditions'=>array(
				'Ticket.sort_top'=>1,
				'Ticket.sys_website_id'=>$SITEID,
			    'Ticket.enable'=>1
			),
			'order'=>'Ticket.sort DESC',
			'limit'=>5
		));
		$this->set('hotTicket',$hotTicket);
		
		// 轮播图-------------------------------------------------------------
		$this->loadModel('SysSiteGallery');
		$gallery = $this->SysSiteGallery->getSiteGallery($SITEID);
		$this->set('gallery',$gallery);
		
		// 票品分类-------------------------------------------------------------
		$this->loadModel('TicketCate');
		$siteTicketCate = $this->TicketCate->getSiteEnable($SITEID);
		$this->set('siteTicketCate',$siteTicketCate);
		
		// 资讯分类-------------------------------------------------------------
		$this->loadModel('ArticleCate');
		$siteArticleCate = $this->ArticleCate->getSiteEnable($SITEID);
		$this->set('siteArticleCate',$siteArticleCate);
		
		// 所有资讯-------------------------------------------------------------
		$allArticle = $this->Article->find('all',array(
			'conditions'=>array(
				'Article.sys_website_id'=>$SITEID,
			    'Article.enable'=>1
			),
			'limit'=>9,
			'order'=>'Article.publish DESC'
		));
		$this->set('allArticle',$allArticle);
		
		// 热门活动-------------------------------------------------------------
		$remen = $this->Article->find('all',array(
				'conditions'=>array(
						'Article.article_cate_id'=>2,
						'Article.sys_website_id'=>$SITEID,
			            'Article.enable'=>1
				),
				'limit'=>7,
				'order'=>'Article.publish DESC'
		));
		$this->set('remen',$remen);
		// 品牌场馆-------------------------------------------------------------
		$this->loadModel('Venue');
		$venues = $this->Venue->getPai($SITEID);
		$this->set('venues',$venues);
		
		// 最新咨询-------------------------------------------------------------
		$this->loadModel('TicketAdvisory');
		$ticketAdvisories = $this->TicketAdvisory->find('all',array(
				'conditions'=>array(
						'TicketAdvisory.ticket_show'=>1,
						'Ticket.sys_website_id'=>$SITEID,
			            'Ticket.enable'=>1
				),
				'limit'=>7,
				'order'=>'TicketAdvisory.created DESC'
		));
		$this->set('ticketAdvisories',$ticketAdvisories);
	}
	
	function autocomplete() {
		$keyword = trim($this->request->data['keyword']);
		$SITEID = Configure::read('SITEID');
		
		//-----------------------------------------------
		$this->loadModel('Venue');
		$this->Venue->recursive = -1;
		$venues = $this->Venue->find('list',array(
			'fields'=>array('Venue.id','Venue.title_cn'),
			'conditions'=>array(
				'Venue.sys_website_id'=>$SITEID,
				'Venue.title_cn like'=>'%'.$keyword.'%'
			),
			'limit'=>5,
			'order'=>'Venue.sort_top DESC,Venue.sort DESC'
		));
		//-----------------------------------------------
		$this->loadModel('Article');
		$this->Article->recursive = -1;
		$articles = $this->Article->find('all',array(
			'fields'=>array('Article.id','Article.title','Article.article_cate_id'),
			'conditions'=>array(
					'Article.sys_website_id'=>$SITEID,
					'Article.title like'=>'%'.$keyword.'%',
			        'Article.enable'=>1
			),
			'limit'=>5,
			'order'=>array('Article.publish DESC')
		));
		//-----------------------------------------------
		$this->loadModel('Ticket');
		$this->Ticket->recursive = -1;
		$tickets = $this->Ticket->find('all',array(
				'fields'=>array('Ticket.id','Ticket.title','Ticket.ticket_cate_id','Ticket.ticket_scate_id'),
				'conditions'=>array(
						'Ticket.sys_website_id'=>$SITEID,
						'Ticket.title like'=>'%'.$keyword.'%',
				        'Ticket.enable'=>1
				),
				'limit'=>5,
				'order'=>array('Ticket.created DESC')
		));
		//-----------------------------------------------
		foreach ($venues as $ik =>$item){
			$data[] = array('id'=>$ik,'cate'=>'v','label'=>$item);
		}
		foreach ($tickets as $item){
			$link = Router::url(TicketView($item));
			$data[] = array(
					'id'=>$item['Ticket']['id'],'cate'=>'t',
					'label'=>$item['Ticket']['title'],
					'link'=>str_replace('/', '_',substr($link,1,strlen($link)))
			);
		}
		foreach ($articles as $item){
			$link = Router::url(ArticleView($item));
			$data[] = array(
					'id'=>$item['Article']['id'],'cate'=>'a',
					'label'=>$item['Article']['title'],
					'link'=>str_replace('/', '_',substr($link,1,strlen($link)))
			);
		}
		//-----------------------------------------------
		//pr($data);
		$this->set('data',$data);
		$this->set('state',true);
	}
	
	/**
	 * dashboard
	 * @author mingxing.zhao@qq.com
	 * @date 2013-12-21 上午11:14:51
	 * @param variable
	 * @return 
	 */
	public function admin_index(){
		$plugins = CakePlugin::loaded();
		array_unshift($plugins,'Core');
		$this->set('plugins',$plugins);
		
		$this->loadModel('User');
		$register_count = $this->User->find('count',array('conditions'=>array('User.created >'=>date('Y-m-d'))));
		$this->set('register_count',$register_count);
		
		$this->loadModel('UserOrder');
		$order_count = $this->UserOrder->find('count',array('conditions'=>array('UserOrder.created >'=>date('Y-m-d'))));
		$this->set('order_count',$order_count);
		
		$this->loadModel('Article');
		$article_count = $this->Article->find('count',array('conditions'=>array('Article.created >'=>date('Y-m-d'))));
		$this->set('article_count',$article_count);
		
		$this->loadModel('Ticket');
		$ticket_count = $this->Ticket->find('count',array('conditions'=>array('Ticket.created >'=>date('Y-m-d'))));
		$this->set('ticket_count',$ticket_count);
		
		$this->loadModel('TicketAdvisory');
		$ticketavisory_count = $this->TicketAdvisory->find('count',array('conditions'=>array('TicketAdvisory.created >'=>date('Y-m-d'))));
		$this->set('ticketavisory_count',$ticketavisory_count);
		
		$this->loadModel('Venue');
		$venue_count = $this->Venue->find('count',array('conditions'=>array('Venue.created >'=>date('Y-m-d'))));
		$this->set('venue_count',$venue_count);
		
		$this->loadModel('TicketBook');
		$ticketbook_count = $this->TicketBook->find('count',array('conditions'=>array('TicketBook.created >'=>date('Y-m-d'))));
		$this->set('ticketbook_count',$ticketbook_count);
		
	}
	
	/**
	 * 基础设置
	 * @author mingxing.zhao@qq.com
	 * @date 2014-1-4 下午9:26:25
	 * @param variable
	 * @return 
	 */
	public function admin_setting(){
		$SITEID = $this->Session->read('SITEID');
		$file = new File(TMP.DS.'cache'.DS.'config'.$SITEID);
		if(!$file->exists()){
			$file->create();
		};
		if ($this->request->is('post')) {
			if($file->write(serialize($this->request->data))){
				$this->Session->setFlash('设置保存成功','tip/tip_float',array('t'=>'success'));
				Cache::delete('sys_website_cache', '_zmx_month_cache_');
			}else{
				$this->Session->setFlash('设置保存失败，请重试','tip/tip_float',array('t'=>'error'));
			} 
		}else{
			$this->request->data = unserialize($file->read());
		}
		
	}
	
	/**
	 * enable plugins
	 * @author mingxing.zhao@qq.com
	 * @date 2013-12-21 上午11:02:46
	 * @param variable
	 * @return 
	 */
	public function admin_plugin(){
		$plugins = CakePlugin::loaded();
		array_unshift($plugins,'Core');
		$this->set('plugins',$plugins);
	}
	
	/**
	 * clear menu cache
	 * @author mingxing.zhao@qq.com
	 * @date 2013-12-21 上午11:13:49
	 * @param variable
	 * @return 
	 */
	public function admin_delmenucache($prower = 0){
		Cache::delete('prower_menu_cache_'.$prower, '_zmx_month_cache_');
		$this->Session->setFlash('菜单缓存已清除','tip/tip_float',array('t'=>'success'));
		$this->redirect($this->request->referer());
	}
}
?>